package cn.livingCloud.controller.core.front;

import cn.livingCloud.dto.front.SelfBindingFrontFormDto;
import cn.livingCloud.service.SelfBindingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.validation.Valid;

/**
 * Created by DingJie on 2016/6/24.
 */
@Controller
@RequestMapping("/front/self_binding")
public class SelfBindingFrontController {
    @Autowired
    private SelfBindingService service;

    @RequestMapping("/binding.htm")
    public String binding(Model model) {
        SelfBindingFrontFormDto formDto = service.loadSelfBindingFrontDto();
        model.addAttribute("formDto", formDto);
        return "front/self_binding/binding";
    }

    @RequestMapping(value = "/binding.htm", method = RequestMethod.POST)
    public String binding(@ModelAttribute("formDto") @Valid SelfBindingFrontFormDto formDto, BindingResult result, Model model) {
        if (result.hasErrors()) {
            return "front/self_binding/binding";
        }
        service.saveSelfBindingFrontFormDto(formDto);
        return "front/self_binding/binding_complete";
    }
}
